package com.to.aboomy.bannersample.pagetransformer;

import com.bumptech.glide.util.LogUtil;
import com.to.aboomy.banner.BannerViewPager;
import com.to.aboomy.bannersample.util.NumberUtil;
import ohos.agp.components.Component;

/**
 * @author xujianhong
 * @since 2021-08-04
 */
public class BackgroundToForegroundTransformer implements BannerViewPager.PageTransformer {
    @Override
    public void transformerPage(BannerViewPager.PageType pageType, Component page, float offset, float offsetPixels) {

        float height = (float) page.getHeight();
        float width = (float) page.getWidth();

        switch (pageType) {
            case PREVIOUS:
                page.setScaleX(1F);
                page.setScaleY(1F);
                page.setPivotX(width);
                page.setPivotY(height);
                page.setTranslationX(1f);
                break;
            case CURRENT:

                if (offsetPixels > 0) {
                    page.setScaleX(1f);
                    page.setScaleY(1f);
                    page.setPivotX(1f);
                    page.setPivotY(1f);
                    page.setTranslationX(1f);
                } else {
                    float scale = min(Math.abs(NumberUtil.sub(1.0F, offset)), 0.5F);
                    page.setScaleX(scale);
                    page.setScaleY(scale);
                    page.setPivotX(NumberUtil.mul(width, 0.5F));
                    page.setPivotY(NumberUtil.mul(height, 0.5F));
                }

                break;
            case NEXT:
                if (offsetPixels > 0) {
                    float scale = min(Math.abs(NumberUtil.sub(1.0F, 1 - offset)), 0.5F);
                    page.setScaleX(scale);
                    page.setScaleY(scale);

                    page.setPivotX(NumberUtil.mul(width, 0.5F));
                    page.setPivotY(NumberUtil.mul(height, 0.5F));
                } else {
                    page.setScaleX(1F);
                    page.setScaleY(1F);
                    page.setPivotX(width);
                    page.setPivotY(height);
                    page.setTranslationX(0f);
                }

                break;
            case OTHER:
                page.setScaleX(1F);
                page.setScaleY(1F);
                page.setPivotX(width);
                page.setPivotY(height);
                page.setTranslationX(0f);
                break;
        }
    }

    static float min(float value, float m) {
        return Math.max(value, m);
    }
}
